/* 
* 版权所有 2015 魔视智能科技(上海)有限公司
* Copyright (c) 2015,Motovis Intelligent Technologies (Shanghai) Co.,Ltd
* 魔视智能公司秘密
* Motovis Intelligent Confidential Proprietary
* 文件名称：update.h
* 摘要: 系统升级头文件
* 版本: V1.0.0.0
* 作者: zhangwenjian
* 完成日期: 2018年3月3日
* 历史记录：
*/

#include "common.h"
#include "Properties.h"

static CProfile  g_tProVersion;
#define VERSION_FILE	"/run/media/mmcblk1p1/version.ini"

void ReadVersionFromIni(DeviceInfoPart *pInfo)
{
	DEBUG("now read version from %s\n", VERSION_FILE);
	
	pInfo->nUpdateFlag = g_tProVersion.GetProperty("", "UpdateFlag", 1);
	pInfo->nBootFlag = g_tProVersion.GetProperty("", "BootFlag", 0);
	g_tProVersion.GetProperty("", "DevSn", pInfo->szDevSn, 32, "no-version");
	g_tProVersion.GetProperty("", "BootVersion", pInfo->szBootVersion, 32, "no-version");
	g_tProVersion.GetProperty("", "WatchDogVersion", pInfo->szWatchDogVer, 32, "no-version");
	
	g_tProVersion.GetProperty("Part0Version", "HardwareVersion", pInfo->aCoreVer[0].szHardwareVer, 32, "no-version");
	g_tProVersion.GetProperty("Part0Version", "SoftwareVersion", pInfo->aCoreVer[0].szSoftwareVer, 32, "no-version");
	g_tProVersion.GetProperty("Part0Version", "DtbVersion", pInfo->aCoreVer[0].szDtbVer, 32, "no-version");
	g_tProVersion.GetProperty("Part0Version", "KernelVersion", pInfo->aCoreVer[0].szKernelVer, 32, "no-version");
	g_tProVersion.GetProperty("Part0Version", "RootfsVersion", pInfo->aCoreVer[0].szRootfsVer, 32, "no-version");
	g_tProVersion.GetProperty("Part0Version", "WeightVersion", pInfo->aCoreVer[0].szWeightVer, 32, "no-version");
	g_tProVersion.GetProperty("Part0Version", "FpgaVersion", pInfo->aCoreVer[0].szFpgaVer, 32, "no-version");
	g_tProVersion.GetProperty("Part0Version", "McuVersion", pInfo->aCoreVer[0].szMcuVer, 32, "no-version");
	g_tProVersion.GetProperty("Part0Version", "AppVersion", pInfo->aCoreVer[0].szAppVer, 32, "no-version");

	g_tProVersion.GetProperty("Part1Version", "HardwareVersion", pInfo->aCoreVer[1].szHardwareVer, 32, "no-version");
	g_tProVersion.GetProperty("Part1Version", "SoftwareVersion", pInfo->aCoreVer[1].szSoftwareVer, 32, "no-version");
	g_tProVersion.GetProperty("Part1Version", "DtbVersion", pInfo->aCoreVer[1].szDtbVer, 32, "no-version");
	g_tProVersion.GetProperty("Part1Version", "KernelVersion", pInfo->aCoreVer[1].szKernelVer, 32, "no-version");
	g_tProVersion.GetProperty("Part1Version", "RootfsVersion", pInfo->aCoreVer[1].szRootfsVer, 32, "no-version");
	g_tProVersion.GetProperty("Part1Version", "WeightVersion", pInfo->aCoreVer[1].szWeightVer, 32, "no-version");
	g_tProVersion.GetProperty("Part1Version", "FpgaVersion", pInfo->aCoreVer[1].szFpgaVer, 32, "no-version");
	g_tProVersion.GetProperty("Part1Version", "McuVersion", pInfo->aCoreVer[1].szMcuVer, 32, "no-version");
	g_tProVersion.GetProperty("Part1Version", "AppVersion", pInfo->aCoreVer[1].szAppVer, 32, "no-version");
}

void WriteVersionToIni(DeviceInfoPart *pInfo)
{
	DEBUG("now update version to %s\n", VERSION_FILE);
	g_tProVersion.SetProperty("", "UpdateFlag", pInfo->nUpdateFlag);
	g_tProVersion.SetProperty("", "BootFlag", pInfo->nBootFlag);
	g_tProVersion.SetProperty("", "DevSn", (const void *)pInfo->szDevSn);
	g_tProVersion.SetProperty("", "BootVersion", (const void *)pInfo->szBootVersion);
	g_tProVersion.SetProperty("", "WatchDogVersion", (const void *)pInfo->szWatchDogVer);
	
	g_tProVersion.SetProperty("Part0Version", "HardwareVersion", (const void *)pInfo->aCoreVer[0].szHardwareVer);
	g_tProVersion.SetProperty("Part0Version", "SoftwareVersion", (const void *)pInfo->aCoreVer[0].szSoftwareVer);
	g_tProVersion.SetProperty("Part0Version", "DtbVersion", (const void *)pInfo->aCoreVer[0].szDtbVer);
	g_tProVersion.SetProperty("Part0Version", "KernelVersion", (const void *)pInfo->aCoreVer[0].szKernelVer);
	g_tProVersion.SetProperty("Part0Version", "RootfsVersion", (const void *)pInfo->aCoreVer[0].szRootfsVer);
	g_tProVersion.SetProperty("Part0Version", "WeightVersion", (const void *)pInfo->aCoreVer[0].szWeightVer);
	g_tProVersion.SetProperty("Part0Version", "FpgaVersion", (const void *)pInfo->aCoreVer[0].szFpgaVer);
	g_tProVersion.SetProperty("Part0Version", "McuVersion", (const void *)pInfo->aCoreVer[0].szMcuVer);
	g_tProVersion.SetProperty("Part0Version", "AppVersion", (const void *)pInfo->aCoreVer[0].szAppVer);

	g_tProVersion.SetProperty("Part1Version", "HardwareVersion", (const void *)pInfo->aCoreVer[1].szHardwareVer);
	g_tProVersion.SetProperty("Part1Version", "SoftwareVersion", (const void *)pInfo->aCoreVer[1].szSoftwareVer);
	g_tProVersion.SetProperty("Part1Version", "DtbVersion", (const void *)pInfo->aCoreVer[1].szDtbVer);
	g_tProVersion.SetProperty("Part1Version", "KernelVersion", (const void *)pInfo->aCoreVer[1].szKernelVer);
	g_tProVersion.SetProperty("Part1Version", "RootfsVersion", (const void *)pInfo->aCoreVer[1].szRootfsVer);
	g_tProVersion.SetProperty("Part1Version", "WeightVersion", (const void *)pInfo->aCoreVer[1].szWeightVer);
	g_tProVersion.SetProperty("Part1Version", "FpgaVersion", (const void *)pInfo->aCoreVer[1].szFpgaVer);
	g_tProVersion.SetProperty("Part1Version", "McuVersion", (const void *)pInfo->aCoreVer[1].szMcuVer);
	g_tProVersion.SetProperty("Part1Version", "AppVersion", (const void *)pInfo->aCoreVer[1].szAppVer);
	g_tProVersion.store();
}

int RebootDevice()
{
	int nVal = 0;
	int nRet = 0;
	int nWatchdogFd = -1;
	
	nWatchdogFd = open("/dev/watchdog0", O_RDWR);
	if (nWatchdogFd < 0) 
	{	
		DEBUG("open /dev/watchdog0 failed!");	
		return -1;
	}

	nVal = WDIOS_ENABLECARD;
	nRet = ioctl(nWatchdogFd, WDIOC_SETOPTIONS, &nVal);
	if (nRet < 0)
	{	
		DEBUG("enabe watchdog failed,nRet = %d", nRet);	
		return -1;
	}

	nVal = 1;
	nRet = ioctl(nWatchdogFd, WDIOC_SETTIMEOUT, &nVal);
	if (nRet < 0)
	{
		DEBUG("set watchdog timeout failed,nRet = %d", nRet);	
		return -1;
	}

	while (1)
	{
		sleep(1);
	}
	
	return 0;
}

int VersionInit(DeviceInfoPart *pInfo)
{
	int nRet = 0;	

	if (access(VERSION_FILE, 0) == -1) 
	{
		if (creat(VERSION_FILE, 0777) < 0) 
		{
			DEBUG("creat %s failed\n", VERSION_FILE);
			return -1;
	    }
		else
		{
			DEBUG("creat %s successed\n", VERSION_FILE);
		}
		
		if (g_tProVersion.load(VERSION_FILE) < 0)
		{
			DEBUG("load %s failed\n", VERSION_FILE);
			return -1;
		}
		WriteVersionToIni(pInfo);
	}
	else
	{
		if (g_tProVersion.load(VERSION_FILE) < 0)
		{
			DEBUG("load %s failed\n", VERSION_FILE);
			return -1;
		}
		if (CheckDeviceInfo(pInfo) == -1)
		{
			ReadVersionFromIni(pInfo);
			//FlashUnlock();
			nRet = SetInfoPart(pInfo);
			//FlashLock();
			if (nRet < 0) 
			{	
				DEBUG("set info part failed\n");
				return nRet;
			}
			RebootDevice();
		}
		else
		{
			WriteVersionToIni(pInfo);
		}
	}
	return nRet;
}


